package com.briup.mt.web.controller;

import com.briup.mt.bean.Article;
import com.briup.mt.bean.BaseUser;
import com.briup.mt.service.IArticleService;
import com.briup.mt.service.IBaseUserService;
import com.briup.mt.utils.ExcelUtils;
import com.briup.mt.utils.Message;
import com.briup.mt.utils.MessageUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Validated
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private IArticleService articleService;
    @GetMapping("query")
    public Message query(String title, Long categoryId){
        List<Article> list =  articleService.query(title,categoryId);
        return MessageUtil.success(list);
    }

    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Article article){
        articleService.saveOrUpdate(article);
        return MessageUtil.success("操作成功");
    }

    @DeleteMapping("deleteById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章编号", required = true, paramType = "query"),
    })
    public Message deleteById(@RequestParam @NotNull Long id){
        articleService.deleteById(id);
        return MessageUtil.success("操作成功");
    }
    @ApiOperation(value = "导入到Excel中", notes = "注意！测试的时候请将地址粘贴到浏览器地址栏测试", produces = "article/octet-stream")
    @GetMapping("download")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "主题", paramType = "query"),
            @ApiImplicitParam(name= "categoryId", value = "分类ID", paramType = "query")}
            )
    public void download(HttpServletResponse response, String title, Long categoryId) throws Exception {
        // 2. 将excel导出
        // 2.1 excel头部数据
        String[] headList = new String[] { "主题", "简介", "文章", "出版时间", "分类ID" };
        // 2.2 属性
        String[] fieldList = new String[] { "title", "brief", "content", "publishTime", "CategoryId"};
        // 2.3 数据列，需要转化
        List<Map<String, Object>> dataList = new ArrayList<>();
        List<Article> list =  articleService.query(title,categoryId);
        for (Article u : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", u.getTitle());
            map.put("brief",u.getBrief());
            map.put("content",u.getContent());
            map.put("publishTime",u.getPublishTime());
            map.put("address",u.getCategoryId());
            dataList.add(map);
        }
        // 2.4 导出文件名
        String excelName = "student_list";
        // 3. 调用工具方法导出Excel
        ExcelUtils.createExcel(response, excelName, headList, fieldList, dataList);

    }
}
